#include "endpointvolumeobject.h"

#include <QDebug>

EndpointVolumeObject::EndpointVolumeObject(QObject *parent)
	: QObject(parent),
	  IAudioEndpointVolumeCallback(),
	  curr_mute(false),
	  curr_vol(100)
	{}

ULONG EndpointVolumeObject::AddRef()
	{
		return InterlockedIncrement(&c_ref);
	}

ULONG EndpointVolumeObject::Release()
	{
		ULONG ul_ref = InterlockedDecrement(&c_ref);
		if (0 == ul_ref)
			this->deleteLater();

		return ul_ref;
	}

HRESULT EndpointVolumeObject::QueryInterface(REFIID riid, void **p_interface)
	{
		if (IID_IUnknown == riid)
		{
			AddRef();
			*p_interface = (IUnknown*)this;
		}
		else if (__uuidof(IAudioEndpointVolumeCallback) == riid)
		{
			AddRef();
			*p_interface = (IAudioEndpointVolumeCallback*)this;
		}
		else
		{
			*p_interface = NULL;
			return E_NOINTERFACE;
		}
		return S_OK;
	}

HRESULT EndpointVolumeObject::OnNotify(PAUDIO_VOLUME_NOTIFICATION_DATA notify)
	{
		bool mute = notify->bMuted;
		int vol = notify->fMasterVolume * 100;

		if (curr_mute != mute)
		{
			curr_mute = mute;
			emit deviceMuteChanged(notify->bMuted);
		}

		if (curr_vol != vol)
		{
			curr_vol = vol;
			emit deviceVolumeChanged(vol);
		}

		return S_OK;

	}
